package models;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Query;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import play.db.jpa.JPA;
import play.db.jpa.Model;

@Entity
public class Episode extends Model{
	
	private Integer season;
	private Integer episode;
	private Integer episodenum;
	private String title;
	private String video;
	private String image;
	
	@Temporal (TemporalType.DATE)
	private Date air_date;
	
	private long tvrageid;
	
	@Lob
	private String synopsis;
	
	@ManyToOne
	private Serie serie;
	
	@OneToMany(mappedBy = "episode", cascade = CascadeType.ALL)
	private List<CommentEpisode> commentEpisodeList;

	
	

	public Episode(Integer season, Integer episode, Integer episodenum, String title, String video,
			String image, Date air_date, String synopsis, Serie serie) {
		super();
		this.season = season;
		this.episode = episode;
		this.episodenum = episodenum;
		this.title = title;
		this.video = video;
		this.image = image;
		this.air_date = air_date;
		this.synopsis = synopsis;
		this.serie = serie;
		this.commentEpisodeList = new ArrayList<CommentEpisode>();
		this.season = season;
		this.episode = episode;
	}

	
	
	

	
	public Integer getSeason() {
		return season;
	}

	public void setSeason(Integer season) {
		this.season = season;
	}

	public Integer getEpisode() {
		return episode;
	}

	public void setEpisode(Integer episode) {
		this.episode = episode;
	}

	public Integer getEpisodenum() {
		return episodenum;
	}

	public void setEpisodenum(Integer episodenum) {
		this.episodenum = episodenum;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getVideo() {
		return video;
	}

	public void setVideo(String video) {
		this.video = video;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public Date getAir_date() {
		return air_date;
	}

	public void setAir_date(Date air_date) {
		this.air_date = air_date;
	}

	public long getTvrageid() {
		return tvrageid;
	}

	public void setTvrageid(long tvrageid) {
		this.tvrageid = tvrageid;
	}

	public String getSynopsis() {
		return synopsis;
	}

	public void setSynopsis(String synopsis) {
		this.synopsis = synopsis;
	}

	public Serie getSerie() {
		return serie;
	}

	public void setSerie(Serie serie) {
		this.serie = serie;
	}

	public List<CommentEpisode> getCommentEpisodeList() {
		return commentEpisodeList;
	}

	public void setCommentEpisodeList(List<CommentEpisode> commentEpisodeList) {
		this.commentEpisodeList = commentEpisodeList;
	}






	public static List<Episode> getAllEpisodes(long serieId){
		
		Query query = JPA.em().createQuery("select e from Episode e where e.serie.id = "
				+ serieId + " order by season desc, episode desc");
		return query.getResultList();
	}
	
	
	public String getFormattedDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String formattedDate = formatter.format(air_date);
		return formattedDate;
	}
	
	
	public static List<Episode> upcomingEpisodes(Date date){
		
		Query query = JPA.em().createQuery("select e from Episode e where e.air_date = :date").setParameter("date", date, TemporalType.DATE);
		return query.getResultList();
	}
	
	public static Episode getEpisode(Serie s, int epNum) {
		
		Episode e = null;
		
		Query query = JPA.em().createQuery("select e from Episode e where e.serie = :serie AND e.episodenum = :epNum").setParameter("serie", s).setParameter("epNum", epNum);
		
		e = (Episode) query.getSingleResult();
		
		return e;
	}

}
